package com.example.howard.xiao7music.ui.Activity.LocalActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.presenter.LocalSongPresenter;
import com.example.howard.xiao7music.service.MusicPlayerService;
import com.example.howard.xiao7music.ui.BottomPlayer;
import com.example.howard.xiao7music.ui.adapter.LocalAdapter.SingerSongAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SingerSongActivity extends Activity {

    private LocalSongPresenter localSongPresenter;
    private List<Map<String, Object>> songlist=new ArrayList<>();
    private String singer;
    private TextView singername;
    private ListView listView;
    private BottomPlayer bottomplayer;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction,transaction_2;
    private MyApplication myApplication;
    private SingerSongAdapter myAdapter;
    public AdapterView.OnItemClickListener listener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singer_song);
        myApplication= (MyApplication) getApplication();
        localSongPresenter=new LocalSongPresenter();
        Intent intent = this.getIntent();
        singer = intent.getStringExtra("SINGER");
        songlist=localSongPresenter.getSongListBySinger(this,singer);
        bottomplayer=new BottomPlayer();
        init();
        refreshListView();
        fragmentManager=getFragmentManager();
        transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.local_singer_bottomplaybar,bottomplayer);
        transaction.commit();
    }

    private void init(){
        singername= (TextView) findViewById(R.id.singer_name);
        listView= (ListView) findViewById(R.id.local_singer_song_list);
        singername.setText(singer);

    }
    private void refreshListView(){
        myAdapter = new SingerSongAdapter(this, songlist, new SingerSongAdapter.SingerSongCallback() {
            @Override
            public void delete(int position) {
                dialog(position);
            }
        });
        listView.setAdapter(myAdapter);
        listener= resetLickListener();
        listView.setOnItemClickListener(listener);
    }

    private AdapterView.OnItemClickListener resetLickListener(){
        AdapterView.OnItemClickListener songListclickListener = new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                myApplication.setSongdatalist(songlist);
                bottomplayer.callByList(arg2);
            }
        };
        return songListclickListener;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBottomPlayerState(bottomplayer);
    }

    @Override
    protected void onPause() {
        super.onPause();
//        transaction_2.remove(bottomPlayer).commit();
        setBottomPlayerState(bottomplayer.getMusicPlayerService(),bottomplayer.getSongList(),
                bottomplayer.getMap(),bottomplayer.getPosition());
    }

    //获取bottomplayer的状态
    private void getBottomPlayerState (BottomPlayer player){
        if (myApplication.musicPlayerService!=null){
            player.setMusicPlayerService(myApplication.musicPlayerService);
        }
        if (myApplication.songdatalist.size()>0){
            player.setSongList(myApplication.songdatalist);
            player.setMap(myApplication.map);
            player.setPosition(myApplication.position);
        }

    }

    //上传bottomplayer的状态
    private void setBottomPlayerState (MusicPlayerService service, List<Map<String, Object>> songlist,
                                       Map<String, Object> map, int pos){
        myApplication.setMusicPlayerService(service);
        myApplication.setSongdatalist(songlist);
        myApplication.setMap(map);
        myApplication.setPosition(pos);
    }

    protected void dialog(final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确认删除指定歌曲吗？");

        builder.setTitle("删除本地歌曲");

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                File remove = new File(songlist.get(position).get("URL").toString());
                remove.delete();
                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                Uri uri = Uri.fromFile(remove);
                intent.setData(uri);
                sendBroadcast(intent);
                dialog.dismiss();
                songlist.remove(position);
                refreshListView();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }
}
